package com.buba.livenuniappservernew.api.login.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.livenuniappservernew.api.MemberUserManagement.entity.MemberUserManagement;
import com.buba.livenuniappservernew.api.MemberUserManagement.mapper.MemberUserManagementMapper;
import com.buba.livenuniappservernew.api.login.service.LoginService;
import com.buba.livenuniappservernew.common.CommonResult;
import com.buba.livenuniappservernew.utils.JwtUtil;
import com.buba.livenuniappservernew.utils.RedisUtil;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private MemberUserManagementMapper memberUserManagementMapper;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 发送验证码
     * @param map
     * @param phone
     * @return
     */
    @Override
    public Boolean send(Map map, String phone) {
        if(StringUtils.isEmpty(phone)) return false;

        Config config = new Config()
                // 阿里AccessKey ID
                .setAccessKeyId("LTAI5tRKbHWNgtgaZ5xMAzZE")
                // 阿里AccessKey Secret	（这两个还不知道的去我前两次关于阿里云的有教程哪里找）
                .setAccessKeySecret("3pG9KvjgGMYN6XiOOg7akgJCXQIgTX");
        // 访问的域名（这个不用变都是这个）
        config.endpoint = "dysmsapi.aliyuncs.com";
        Client client = null;
        try {
            client = new Client(config);
            SendSmsRequest request = new SendSmsRequest();

            request.setSignName("阿里云短信测试");//签名名称
            request.setTemplateCode("SMS_298595309");//模版Code
            request.setPhoneNumbers(phone);//电话号码
            //这里的参数是json格式的字符串
            request.setTemplateParam(JSONObject.toJSONString(map));
            SendSmsResponse response = client.sendSms(request);
            System.out.println("发送成功："+new Gson().toJson(response));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 手机号登录
     * @param memberUserManagement
     * @return
     */

    @Override
    public CommonResult userPhoneLogin(MemberUserManagement memberUserManagement) {
        QueryWrapper<MemberUserManagement> wrapper = new QueryWrapper<>();
        wrapper.eq("member_user_phone",memberUserManagement.getMemberUserPhone());
        MemberUserManagement one = memberUserManagementMapper.selectOne(wrapper);

        if (one == null){
            return CommonResult.error(500,"暂无此手机号信息");
        }
        String jwt = JwtUtil.createJWT(one.getId().toString());
        redisUtil.set("手机号："+memberUserManagement.getMemberUserPhone(),jwt);
        return CommonResult.success(jwt);
    }
    /**
     *用户账号密码登录
     * @param memberUserManagement
     * @return
     */
    @Override
    public CommonResult userPasswordLogin(MemberUserManagement memberUserManagement) {
        return null;
    }
}
